package com.abu.course.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import com.abu.common.Resp;
import com.abu.course.pojo.Course;
import com.abu.course.service.CourseService;
import com.abu.course.vo.ClassroomOrderVO;
import com.abu.course.vo.QueryConditionSelect;
import com.abu.course.vo.QueryCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("noAuthorize/getCourseListByCondition")
    public Resp getClassroomListByCondition(QueryCourseVo queryCourseVo) {
        HashMap<String, Object> resultHashMap = this.courseService.queryPageByCondition(queryCourseVo);
        return Resp.ok().data(resultHashMap);
    }

    @GetMapping("noAuthorize/queryPageOnlyCourse")
    public Resp queryPageOnlyCourse(QueryCourseVo queryCourseVo) {
        HashMap<String, Object> resultHashMap = this.courseService.queryPageOnlyCourse(queryCourseVo);
        return Resp.ok().data("pageResult", resultHashMap);
    }

    @GetMapping("noAuthorize/getCourseByClassroomName")
    public Resp getClassroomOrderByClassroomName(@RequestParam("classroomName") String classroomName) {
        List<ClassroomOrderVO> courseList = this.courseService.getClassroomOrderByClassroomName(classroomName);
        if (CollectionUtils.isEmpty(courseList)) {
            return Resp.error().msg("当前教室没有安排课程");
        }
        return Resp.ok().data("courseList", courseList);
    }

    @GetMapping("noAuthorize/getCourseById/{id}")
    public Resp getCourseById(@PathVariable("id") Long id) {
        Course course = this.courseService.getCourseById(id);
        if (ObjectUtil.isNotNull(course)) {
            return Resp.ok().data("course", course);
        }
        return Resp.error().msg("教室资源不存在");
    }

    @GetMapping("noAuthorize/all-history")
    public Resp queryAllHistory(QueryConditionSelect queryConditionSelect) {
        HashMap<String, Object> mapResult = this.courseService.queryAllHistory(queryConditionSelect);
        return Resp.ok().data("pageResult", mapResult);
    }


    @GetMapping("noAuthorize/history")
    public Resp queryHistoryByCondition(QueryConditionSelect queryConditionSelect) {
        HashMap<String, Object> mapResult = this.courseService.queryHistoryByCondition(queryConditionSelect);
        return Resp.ok().data("pageResult", mapResult);
    }


    @GetMapping("noAuthorize/checkCourseIsAvailable")
    public Resp checkDataIsAvailable(
            @RequestParam("classroomId") Long id,
            @RequestParam("beginTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") LocalDateTime beginTime,
            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") LocalDateTime endTime
    ) {
        Boolean flag = this.courseService.checkData(id, beginTime, endTime);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().msg("时间有冲突，请更换教室或时间段");
    }

    @PostMapping("addCourse")
    public Resp addCourse(@RequestBody Course course) {
        Boolean flag = this.courseService.saveCourse(course);
        if (flag) {
            return Resp.ok().msg("添加成功");
        }
        return Resp.error().msg("添加失败");
    }


    @PutMapping("updateCourse")
    public Resp modifiedClassroom(Course course) {
        Boolean flag = this.courseService.modifiedClassroom(course);
        if (flag) {
            return Resp.ok().msg("修改成功");
        }
        return Resp.error().msg("修改失败");
    }

    @DeleteMapping("deleteCourse/{id}")
    public Resp deleteCourse(@PathVariable("id") Long id) {
        boolean flag = this.courseService.deleteCourse(id);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().msg("删除成功");
    }

}

